package com.wnhz.wnmap.order.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.wnhz.wnmap.audit.api.AuditFeign;
import com.wnhz.wnmap.domain.entity.Order;
import com.wnhz.wnmap.order.dao.IOrderDao;
import com.wnhz.wnmap.order.service.IOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderDao orderDao;
    @Autowired
    private AuditFeign auditFeign;

    @GlobalTransactional
    @Override
    public void orderSeats(long auditId, int seats) {
        Order order = new Order();
        order.setAudit(auditId);
        order.setSeats(seats);
        order.setCinema(1L);

        log.debug("准备生成订单............");
        orderDao.insert(order);
        log.debug("数据库生成订单成功......");


        log.debug("开启分布式事务，执行订座流程.....");
        auditFeign.reduceSeats(auditId, seats);
        Snowflake snowflake = new Snowflake();
        long orderId = snowflake.nextId();
        order.setNum(orderId);
        orderDao.updateById(order);
        log.debug("订座流程结束，产生订单号，更新订单完成......");


    }
}
